package someTestExcemple.thread;

import lombok.Data;
import lombok.Getter;

@Data
public class Account {
    private static String cardId;
    private double money;

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }

    public Account() {
    }
    //2. 同步方法保证线程安全 -可读性好-但是锁的范围更大
    public synchronized void drawAmount(double drawAmount) {
        //1.同步代码块的方式保证线程安全
        synchronized (this)
         {
            System.out.println(Thread.currentThread().getName() + "取钱线程启动...当前余额:" + this.money);
            if (this.money >= drawAmount) {
                System.out.println(Thread.currentThread().getName() + "取钱成功，吐出钞票：" + drawAmount);
                this.money -= drawAmount;
                System.out.println(Thread.currentThread().getName() + "账户余额为：" + this.money);
            } else {
                System.out.println(Thread.currentThread().getName() + "只有"+ this.money + "元。取钱失败，账户余额不足!");
            }
        }
    }
}
